package concurrentV;

import syn_demo1.Counter;

import javax.swing.table.TableRowSorter;

public class TwoThreadPrint12ab {
    private static final Object lock = new Object();
    private  static  boolean isNum = true;// 先打印数字 true：打印数字 false：打印字母



    private void printerNum(){
        for (int i = 1; i < 101; ) {
            synchronized (lock){
                if(!isNum){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (int j = 0; j < 2; j++) {
                    System.out.println(Thread.currentThread().getName() +": "+ i);
                    i++;
                }
                isNum = false;
                lock.notifyAll();
            }
        }
    }

    private void printerABC(){
        for ( char c = 'A'; c <= 'Z' ; c++) {
            synchronized (lock){
                if(isNum){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + " :" + c);
                isNum = true;
                lock.notifyAll();
            }
        }
    }

    public static void main(String[] args) {
        TwoThreadPrint12ab print12ab = new TwoThreadPrint12ab();
        new Thread(() -> print12ab.printerNum(),"thread1").start();
        new Thread(() -> print12ab.printerABC(),"thread2").start();
    }
}
